/*----------------------------------------------------------------------------

	Copyright (c) 2006, Creator of the Cant Project.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	+ Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	+ Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	+ Neither the name of the Creator of the Cant Project nor the names of its
	contributors may be used to endorse or promote products derived from this
	software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------*/

#ifndef	_FILETAG_HPP_
#define	_FILETAG_HPP_

#include	<string>

#include	"../except.hpp"
#include	"../projtag.hpp"
#include	"../tiny/tinyxml.h"
#include	"../../util/log.hpp"
#include	"../../util/util.hpp"



/**
	File tag.


	@version	0.1
	@author		<a href="mailto:cant.project@gmail.com">Mikey</a>
*/
class FileTag	: public Tag
{
	private:
		/**
			Attribute name.
			
		*/
		static const std::string			ATTRIB_PATH;
		static const std::string			ATTRIB_USER;
		static const std::string			ATTRIB_PASSWORD;
		
		
		/**
			Protocol/URI separator.
			
		*/
		static const std::string			SEPARATOR;
		
		
		/**
			File loading protocol.
			
		*/
		static const std::string			PROTOCOL_FILE;
		static const std::string			PROTOCOL_FTP;
		static const std::string			PROTOCOL_HTTP;
		static const std::string			PROTOCOL_HTTPS;
	
	
	public:
		/**
			Supported protocols.
			
		*/
		enum Protocol
		{
			unknown			= 0,
			file,
			ftp,
			http,
			https
		};
		
		
	private:
		/**
			The path to the file.
			
			@see		getPath
		*/
		std::string				m_strPath;
		
		
		/**
			User name.
			
			Some protocols may require authentication.
			
			@see		getUser
		*/
		std::string				m_strUser;
		
		
		/**
			Password.
			
			Some protocols may require authentication.
			
			@see		getPassword
		*/
		std::string				m_strPassword;
		
		
		/**
			Protocol part of the <code>path</code> attribute.
			
			@see		getProtocol
		*/
		Protocol				m_protocol;
		
		
		/**
			Address part of the 
			
			@see		getAddress
		*/
		std::string				m_strAddress;
		
		
	public:
		/**
			Constructor.

		*/
		FileTag(TiXmlElement* a_pElement );
		
		
		/**
			Copy constructor.
		
		*/
		FileTag(const FileTag& a_other );
		
		
		/**
			Assignment operator.
			
		*/
		FileTag&	operator=(const FileTag& a_other );
		
		
		/**
			Clone.
			
		*/
		Tag*	clone(void ) const;


		/**
			Executes this tag.

			@param		a_project		containing project.
			@param		a_target		containing target.
		*/
		void	execute(Project& a_project, ProjectTarget& a_target );
		
		
		/**
			Gives the value of the <code>path</code> attribute.
			
		*/
		std::string	getPath(void ) const;
		
		
		/**
			Gives the value of the <code>user</code> attribute.
			
		*/
		std::string	getUser(void ) const;
		
		
		/**
			Gives the value of the <code>password</code> attribute.
			
		*/
		std::string	getPassword(void ) const;
		
		
		/**
			Gives the protocol part of the <code>path</code> attribute.
			
		*/
		Protocol	getProtocol(void ) const;
		
		
		/**
			Gives the address part of the <code>path</code> attribute.
			
		*/
		std::string	getAddress(void ) const;

		
	protected:
		/**
			Copies the 'file' specific parts of the object.
			
		*/
		void	copyFile(const FileTag& a_other );
		
		
	private:
		/**
			Splits a path into 'protocol' and 'address' parts.
			
		*/
		void	splitPath(void );
};



//--------------------------------------------------------------------------//



/**
	Creates the <code>FileTag</code> objects.


	@version	0.1
	@author		<a href="mailto:cant.project@gmail.com">Mikey</a>
*/
class FileTagCreator	: public ITagCreator
{
	private:
		/**
			Constant.

		*/
		const static std::string			TAG_NAME_FILE;


	public:
		/**
			Default constructor.
			
			Registers itself with <code>TagFactory</code>.
		*/
		FileTagCreator(void );
		
		
		/**
			Creates a typed <code>Tag</code> object.

			Implementation for the <code>ITagCreator</code> interface.

			@param		a_pElement		xml element.
			@param		a_strName		name of required tag.
			@return		typed <code>Tag</code> object if name is recognised,
						<code>null</code> otherwise.
		*/
		Tag*	createTag(TiXmlElement* a_pElement, const std::string& a_strName );
};

#endif
